package com.iwhalecloud.bss.kite.cucc.service.inf;

import java.util.Map;

import com.iwhalecloud.bss.kite.common.util.KiteMapUtils;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.common.constant.LocalKeyConsts;
import com.iwhalecloud.bss.kite.common.util.GrayUtil;
import com.iwhalecloud.bss.kite.dataservice.cache.DcPublicCache;

/**
 * @ClassName : InfServiceHelper
 * @Description : 接口调用辅助工具
 * @Author : xie.zhengjun
 * @Date : 2021/4/26 21:34
 */
public class InfServiceHelper {
    public static final String CALL_MODE_HTTP = "HTTP";

    public static final String CALL_MODE_HSF = "HSF";

    public static final String OP_CODE_KEY = "OP_CODE";

    /**
     * 判断服务提供方式是否为HTTP
     *
     * @param opCode 能开接口编码
     * @return 是HTTP则返回true，否则返回false
     */
    public static boolean byHttp(String opCode) {
        String codea = DcPublicCache.getCodea(LocalKeyConsts.ABILITY_INFO_STYPE, opCode);
        return KiteStringUtils.isEqual(codea, CALL_MODE_HTTP);
    }

    /**
     * 判断服务提供方式是否为HSF
     *
     * @param opCode 能开接口编码
     * @return 是HSF则返回true，否则返回false
     */
    public static boolean byHsf(String opCode) {
        String codea = DcPublicCache.getCodea(LocalKeyConsts.ABILITY_INFO_STYPE, opCode);
        return KiteStringUtils.isEqual(codea, CALL_MODE_HSF);
    }

    /**
     * 添加灰度路由信息
     *
     * @param fullMap OP_CODE所在的map
     * @param reqMap key为***REQ的map，一般位于UNI_BSS_BODY下
     */
    public static void adaptGray(Map<String,Object> fullMap, Map<String,Object> reqMap) {
        if (GrayUtil.isGray()) {
            String opCode = KiteMapUtils.getString(fullMap, OP_CODE_KEY);
            if (KiteStringUtils.isEmpty(opCode)) {
                return;
            }
            if (InfServiceHelper.byHsf(opCode)) {
                GrayUtil.addControlNode(reqMap);
            }
            else {
                GrayUtil.addHeaderForGray(fullMap);
            }
        }
    }
}
